﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.Application
{
	public partial class StatementSourceUserControl : UserControl, INotifyPropertyChanged
	{
		private readonly string textSetupLinkLabel;
		private StatementSource[] statementSources;

		public StatementSourceUserControl()
		{
			this.InitializeComponent();

			this.textSetupLinkLabel = this.tableLayoutPanel.GetControlFromPosition(2, 0).Text;
		}

		[EditorBrowsable(EditorBrowsableState.Always),
		 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true)]
		public StatementSource[] StatementSources
		{
			get { return this.statementSources; }
			set
			{
				if (value != this.statementSources)
				{
					this.statementSources = value;

					this.tableLayoutPanel.Controls.Clear();

					int row = 0;
					foreach (StatementSource statementSource in this.statementSources)
					{
						var labelName = new Label {Text = statementSource.DisplayName, AutoSize = true};
						var linkLabelLastUpdate = new LinkLabel {AutoSize = true};
						var linkLabelSetup = new LinkLabel {AutoSize = true};

						linkLabelLastUpdate.Text = "Last updated on...";
						linkLabelSetup.Text = this.textSetupLinkLabel;
						linkLabelLastUpdate.LinkClicked += this.linkLabelLastUpdate_LinkClicked;
						linkLabelSetup.LinkClicked += this.linkLabelSetup_LinkClicked;

						this.tableLayoutPanel.Controls.Add(labelName, 0, row);
						this.tableLayoutPanel.Controls.Add(linkLabelLastUpdate, 1, row);
						this.tableLayoutPanel.Controls.Add(linkLabelSetup, 2, row);

						++row;
					}

					this.FirePropertyChanged("StatementSources");
				}
			}
		}

		private void linkLabelSetup_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			throw new NotImplementedException();
		}

		private void linkLabelLastUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			throw new NotImplementedException();
		}

		private void FirePropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#region Implementation of INotifyPropertyChanged

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}
}